package cn.go.buster.launcher.utils;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.go.buster.event.EventCard;
import cn.go.buster.launcher.api.EventCenter;

/**
 * @author logcat
 * @date 2020/07/20
 */
public class MethodUtils {

    private static volatile  MethodUtils instance;

    public static MethodUtils getInstance(){
        if(instance == null){
            synchronized (MethodUtils.class){
                if(instance == null){
                    instance = new MethodUtils();
                }
            }
        }
        return instance;
    }


    private Handler handler;
    private  Handler mainThreadHandler;

    private MethodUtils(){
        handler = new Handler();
        mainThreadHandler = new Handler(Looper.getMainLooper());
    }




    /**
     * 获取方法中各个参数的返回值类型
     * @param eventCard
     * @return
     */
    public Class[] getMethodValueTypes(EventCard eventCard) {
        String[] split = eventCard.getParamTypes().split(",");
        List<Class> typeList = new ArrayList<>();
        for (String typeName : split) {
            if(TextUtils.isEmpty(typeName)){
                continue;
            }
            try {
                Class typeClass = Class.forName(typeName);
                typeList.add(typeClass);
            } catch (ClassNotFoundException e) {
                EventCenter.logger.error("Class " + typeName + " is not found.");
            }
        }

        return typeList.toArray(new Class[]{});
    }

    /**
     * 判断两个方法的参数类型是否一致
     * @param parameterTypes
     * @param methodValueTypes
     * @return
     */
    public boolean isSameParameter(Class<?>[] parameterTypes, Class<?>[] methodValueTypes) {
        if(parameterTypes == null || methodValueTypes == null){
            return false;
        }

        if(parameterTypes.length != methodValueTypes.length){
            return false;
        }

        int length = parameterTypes.length;
        for (int i = 0; i < length; i++) {
            if(parameterTypes[i] != methodValueTypes[i]){
                return false;
            }
        }

        return true;
    }


    /**
     * 在主线程中执行某方法
     * @param method
     * @param obj
     * @param params
     */
    public void runOnMainThread(Method method, Object obj, Object... params) {
        runOnMainThread(new Runnable() {
            @Override
            public void run() {
                try {
                    method.invoke(obj, params);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * 在主线程中执行
     * @param runnable
     */
    private void runOnMainThread(Runnable runnable){
        if(Looper.getMainLooper().getThread() != Thread.currentThread()){
            mainThreadHandler.post(runnable);
        }else{
            handler.post(runnable);
        }
    }

}
